#include <iostream>
#include "cbbuildcfg.h"
#include "stlconvert.h"
#include "tinyxml.h"

CCodeBlocksBuildConfig::CCodeBlocksBuildConfig( void ) {
  Clear();
}

CCodeBlocksBuildConfig::~CCodeBlocksBuildConfig( void ) {
  Clear();
}

void CCodeBlocksBuildConfig::Clear( void ) {
  m_Platforms.Clear();
  m_ToolChains.Clear();
  m_GlobalVariables.Clear();
  m_DefaultOptions.Clear();
  m_Targets.Clear();
  m_TargetNameCase = CASE_KEEP;
  m_MacroVariableCase = CASE_KEEP;
  m_QuotePathMode = QUOTE_AUTO;
  m_NumericToolchainSuffix = true;
  m_FlatObjectNames = false;
  m_FlatObjectPaths = false;
  m_MultilineObjects = false;
  m_MultilineOptions = false;
  m_IncludeDependencies = false;
  m_KeepObjectDirectories = false;
  m_KeepOutputDirectories = false;
  m_BeQuiet = false;
  m_BeVerbose = false;
}

bool CCodeBlocksBuildConfig::Load( const CString& FileName ) {
  Clear();
  m_ToolChains.AddDefault();
  m_GlobalVariables.AddDefault();
  TiXmlDocument cfg;
  if( !cfg.LoadFile( FileName.GetCString() ) ) {
    return false;
  }
  const TiXmlElement *root = cfg.RootElement();
  if( 0 == strcmp( root->Value(), "cbp2make" ) ) {
    const TiXmlNode *_toolchains = root->FirstChild( "toolchains" );
    if( 0 != _toolchains ) {
      const TiXmlElement *toolchains = _toolchains->ToElement();
      if( 0 != toolchains ) {
        m_ToolChains.Read( toolchains );
      }
    }
    const TiXmlNode *_globvars = root->FirstChild( "globalvariables" );
    if( 0 != _globvars ) {
      const TiXmlElement *globvars = _globvars->ToElement();
      if( 0 != globvars ) {
        m_GlobalVariables.Read( globvars );
      }
    }
    const TiXmlNode *_options = root->FirstChild( "options" );
    if( 0 != _options ) {
      const TiXmlElement *options = _options->ToElement();
      if( 0 != options ) {
        char *value = 0;
        if( ( value = ( char * )options->Attribute( "default" ) ) ) {
          m_DefaultOptions = value;
        }
      }
    }
  }
  return true;
}

bool CCodeBlocksBuildConfig::Save( const CString& FileName ) {
  m_ToolChains.AddDefault();
  m_GlobalVariables.AddDefault();
  TiXmlDocument cfg;
  TiXmlDeclaration *xmld = new TiXmlDeclaration( "1.0", "", "" );
  cfg.LinkEndChild( xmld );
  TiXmlElement *root = new TiXmlElement( "cbp2make" );
  cfg.LinkEndChild( root );
  TiXmlElement *toolchains = new TiXmlElement( "toolchains" );
  root->LinkEndChild( toolchains );
  m_ToolChains.Write( toolchains );
  TiXmlElement *globvars = new TiXmlElement( "globalvariables" );
  root->LinkEndChild( globvars );
  m_GlobalVariables.Write( globvars );
  TiXmlElement *options = new TiXmlElement( "options" );
  root->LinkEndChild( options );
  options->SetAttribute( "default", m_DefaultOptions.GetCString() );
  return cfg.SaveFile( FileName.GetCString() );
}

void CCodeBlocksBuildConfig::Show( void ) {
  m_Platforms.Show();
  m_ToolChains.Show();
  m_GlobalVariables.Show();
}

//------------------------------------------------------------------------------
